import screenlets
import sys
import re
import gobject
import gettext
from datetime import datetime
import commands
import time
import os
import subprocess
import gtk
# translation stuff
gettext.textdomain('screenlets')
gettext.bindtextdomain('screenlets', screenlets.INSTALL_PREFIX +  '/share/locale')

def _(s):
	return gettext.gettext(s)




def get_net_ip():
	"""Returns   distant   ip addr"""
	from urllib2 import urlopen
	try:
		ip = urlopen("htttp://metalgun.dyndns.org/what_is_my_ip.php").read()
	except:	
		try:		
			ip =  urlopen("http://www.whatismyip.com/automation/n09230945.asp").read()
		except:
			return "IP detection failed"	
	return ip

def net_get_ping():
	site= ['metalgun.dyndns.org','backupmg.homelinux.org','baloo.dyndns.org','bandoine.hd.free.fr','hikabox.com','www.supinfo.com']
	time= []
	i=0
	aff=''
	regex=re.compile("[0-9.]+")	
	for element in site:
			ping = commands.getoutput("perl '/usr/share/screenlets/Sysmonitor/site.pl' 2 http "+element)
			reponse = ping.split(' ')
			test = regex.match(reponse[2],1)
			if (str(test) !="None"):
				time.append(reponse[2])
			else : time.append("HS")

	while i < len(site) :
		aff += str(site[i])
		if len(site[i])< 16 : aff += "		"
		elif len(site[i])< 24: aff += "	"
		else : aff +=" "
		test = regex.match(time[i],1)
		if (str(test) !="None"):
			aff += str(round(float(time[i]),2))
		else : aff += str(time[i])
		if i!= len(site)-1 :aff +='\n'	
		i = i + 1
	return aff


def cpu_get_temp():
	com = commands.getoutput("cat /proc/acpi/thermal_zone/THM/temperature")
	com2 = com.split(' ')
	temp = com2[13]
	return temp	






#print get_net_ip()
#print net_get_ping()
#print cpu_get_temp()
